import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PennerInfo {
	String pennerName;
	String pennerBild;
	double cash;
	String sCash;
	double permil;
	String sPermil;
	int studyTime;
	String sStudyTime;
	int attTime;
	String sAttTime;
	int crapShare;
	String sCrapShare;
	int crapChk;
	String sCrapChk;
	int craps;
	String sCraps;
	PennerInfo pI;
	Timer studyTimer = null;
	Timer attTimer = null;
	PennerStats stats;

	PennerInfo(PennerStats pStats) {
		stats = pStats;
		resetInfo();
	}
	
	public void resetInfo() {
		pennerName = "Pennername";
		pennerBild = "";
		cash = 0;
		permil = 0;
		studyTime = 0;
		attTime = 0;
		crapShare = 0;
		craps = 0;
	}
	
	public void getInfo(Connection pennerConn) {
		pI = this;
		String sPage = "";
		
		try {
//			   while(pennerConn.gettingPage) {
//				   try {
//					Thread.sleep(500);
//				   } catch (InterruptedException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
//				   }
//			   }
			   sPage = pennerConn.getPage(pennerConn.sSideURL+pennerConn.sInfoURL);
		} catch (IOException e) {
        	stats.setStatus("Info Fehler");
		}

		this.pennerName = pennerConn.sUID;

	    Pattern p = Pattern.compile("&euro;(.*?)[ \t\r\n]*</a>");
	    Matcher matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sCash = sPage.substring(matcher.start(1), matcher.end(1));
	    	p = Pattern.compile("[^0-9]");
	    	matcher = p.matcher(this.sCash);
	    	this.sCash = matcher.replaceAll("");
	    }

		//<img class="avatar" alt="Avatar" title="Avatar" src="http://inodes.pennergame.de/bl_DE/avatare/2269822.jpg" >
	    p = Pattern.compile("<img class=\"avatar\".*title=\"Avatar\" src=\"(.*?)\" >[ \t\r\n]*</div>");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	try {
				pennerConn.getPennerImage(sPage.substring(matcher.start(1), matcher.end(1)));
			} catch (IOException e) {
				
			}
	    }	    	
	    //<span class=\'promille_.*\'>[ \t\r\n]*(.*?)&permil;</span>
	    p = Pattern.compile("<li class=\"icon beer\">[ \t\r\n]*<a href=\"/stock/\"[ \t\r\n].*>[ \t\r\n]*([.0-9]*)[ \t\r\n]*");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sPermil = sPage.substring(matcher.start(1), matcher.end(1));
	    	p = Pattern.compile("[^0-9]");
	    	matcher = p.matcher(this.sPermil);
	    	this.sPermil = matcher.replaceAll("");
	    }
		
	    p = Pattern.compile("<span>[ \t\r\n]*([0-9]*)[ \t\r\n].*[ \t\r\n]*<font id=\"wirkung\">");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sCraps = sPage.substring(matcher.start(1), matcher.end(1));
	    }

	    p = Pattern.compile("<font id=\"wirkung\">[ \t\r\n]*Preis:[ \t\r\n]*(<strong>)?[^;]+;(.*?)[ \t\r\n]*(</strong>)?</font>");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sCrapShare = sPage.substring(matcher.start(2), matcher.end(2));
	    	p = Pattern.compile("^[0,]*([0-9]*)");
	    	matcher = p.matcher(this.sCrapShare);
	    	if (matcher.find()) {
	    		this.sCrapShare = this.sCrapShare.substring(matcher.start(1), matcher.end(1));
	    	}
	    }

	    p = Pattern.compile("<input id=\"chkval\" type=\"hidden\" name=\"chkval\" value=\"([0-9]*)\" />");
	    //<span id=\"pfandflaschen_kurs_ajax\">([0-9]*)</span>");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sCrapChk = sPage.substring(matcher.start(1), matcher.end(1));
	    }
	    
	    //<li class="book"><script language='javascript'>counter(6567)</script></li>
	    //<li class=\"book\"><script language=\'javascript\'>[^\\-0-9]*([\\-0-9]*).*</script></li>");
	    p = Pattern.compile("<li class=\"icon book\">[ \t\r\n]*<a href=\"/skills/\"[ \t\r\n].*>[ \t\r\n]*<script type=\"text/javascript\">[^\\-0-9]*([\\-0-9]*).*</script>");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sStudyTime = sPage.substring(matcher.start(1), matcher.end(1));
	    }
		//<li class="att"><script language='javascript'>counter(3321)</script></li>
	    //<li class=\"att\"><script language=\'javascript\'>[^\\-0-9]*([\\-0-9]*).*</script></li>
	    p = Pattern.compile("<li class=\"icon fight\">[ \t\r\n]*<a href=\"/fight/\"[ \t\r\n].*>[ \t\r\n]*<script type=\"text/javascript\">[^\\-0-9]*([\\-0-9]*).*</script>");
	    matcher = p.matcher(sPage);
	    if (matcher.find()) {
	    	this.sAttTime = sPage.substring(matcher.start(1), matcher.end(1));
	    }
	    
		this.cash = (double) Integer.valueOf(this.sCash) / 100;
		this.permil = (double) Integer.valueOf(this.sPermil) / 100;
		this.studyTime = Integer.valueOf(this.sStudyTime);
		if (studyTimer == null) {
			if (this.studyTime < 0) {
				this.studyTime=0;
			} else {
				studyTimer = new Timer();
				TimerTask studyTask = new TimerTask() {
					public void run() {
						if (pI.studyTime < 0) {
							studyTimer.cancel();
							studyTimer=null;
						} else { 
							pI.studyTime=pI.studyTime-1;
						}
					}
				};
				//start des Timers:
				studyTimer.scheduleAtFixedRate(studyTask, 0, 1000);
				// wiederholt sich unendlich immer nach einer Sekunde (1000 Millisekunden)
			}
		}
		
		this.attTime = Integer.valueOf(this.sAttTime);
		if (attTimer==null) {
			if (this.attTime < 0) {
				this.attTime=0;
			} else {
				attTimer = new Timer();
				TimerTask attTask = new TimerTask() {
					public void run() {
						if (pI.attTime < 0) {
							attTimer.cancel();
							attTimer=null;
						} else {
							pI.attTime=pI.attTime-1;
						}
					}
				};
				//start des Timers:
				attTimer.scheduleAtFixedRate(attTask, 0, 1000);
				// wiederholt sich unendlich immer nach einer Sekunde (1000 Millisekunden)
			}
		}
		this.crapShare = Integer.valueOf(this.sCrapShare);
		this.crapChk = Integer.valueOf(this.sCrapChk);
		this.craps = Integer.valueOf(this.sCraps);
	}
	

}
